﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CDB.LinqModel.Data;
using CDB.LinqData;
using CDB.LinqModel;

public partial class myAccount : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    Property property;
    List<Property> properties = new List<Property>();
    Builder builder;
    Architect architect;
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["username"] != null)
            Session["User"] = Request.QueryString["username"].ToString();

        if (Session["User"] != null)
            FillDetails(Session["User"].ToString());
    }
    public void FillDetails(string userName)
    {
        User nUser = new User(_dataObject);
        nUser.UserName = userName;
        nUser.GetUserByUserName();

        if (nUser.Id != 0)
        {
            tdName.InnerText = nUser.FirstName + " " + nUser.LastName;
            tdEmail.InnerText = nUser.Email;
            tdPhone.InnerText = nUser.Phone;
            tdAddress.InnerHtml = nUser.Address1 + " " + nUser.Address2 + ", <br />" + nUser.City + "<br />" + nUser.GetStateById(Convert.ToInt32(nUser.State)).Name + "<br />" + nUser.Zip + "<br />" + nUser.GetCountryById(Convert.ToInt32(nUser.Country)).Name;
            tdPropertyLocation.InnerText = nUser.PropertyLocation;
            tdServiceRequired.InnerText = nUser.ServiceRequired;

            fillBuilders(nUser.Id);
            fillArchitects(nUser.Id);
        }
        
    }
    private void fillBuilders(int userId)
    {
        property = new Property(_dataObject);
        properties = property.GetAllPropertiesByUserId(userId);

        List<int> builders = new List<int>();

        if (properties.Count > 0)
        {
            for (int i = 0; i < properties.Count; i++)
            {
                if (properties[i].BuilderId != null)
                {
                    builders.Add(Convert.ToInt32(properties[i].BuilderId));
                }
            }
        }

        builders = builders.Distinct().ToList();

        for (int j = 0; j < builders.Count; j++)
        {
            builder = new Builder(_dataObject);
            builder.Id = builders[j];
            builder.GetBuilderById();

            if (builder.Id != 0)
            {
                ltrBuilder.Text += builder.Name + "<br />";
            }
        }
    }
    private void fillArchitects(int userId)
    {
        property = new Property(_dataObject);
        properties = property.GetAllPropertiesByUserId(userId);

        List<int> architects = new List<int>();

        if (properties.Count > 0)
        {
            for (int i = 0; i < properties.Count; i++)
            {
                if (properties[i].ArchitectId != null)
                {
                    architects.Add(Convert.ToInt32(properties[i].ArchitectId));
                }
            }
        }

        architects = architects.Distinct().ToList();

        for (int j = 0; j < architects.Count; j++)
        {
            architect = new Architect(_dataObject);
            architect.Id = architects[j];
            architect.GetArchitectById();

            if (architect.Id != 0)
            {
                ltrArchitect.Text += architect.Firm + "<br />";
            }
        }
    }
}